package com.think.teaching.controller;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Question;
import com.think.teaching.service.impl.QuestionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.xml.crypto.dsig.keyinfo.KeyValue;

import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 试题 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "题库管理接口")
public class QuestionController {

	private final QuestionServiceImpl questionService;

	public QuestionController(QuestionServiceImpl questionService) {
		this.questionService = questionService;
	}

	@ApiOperation("添加新的试题")
	@PostMapping("admin/question")
	public Result AddNewQuestion(@RequestBody() Question data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Question question;
			try {
				question = questionService.AddQuestion(data);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR,e.getMessage());
			}
			if (question != null) {
				return Result.success(question);
			} else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新试题")
	@RequestMapping(value = "admin/question", method = RequestMethod.PUT)
	public Result UpdateNewQuestion(@RequestBody() Question data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Question question;
			try {
				question = questionService.UpdateQuestion(data);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
			if (question != null) {
				return Result.success(question);
			} else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除试题")
	@DeleteMapping("admin/question/{id}")
	public Result DeleteNewQuestion(@PathVariable("id") Integer id) {
		Map<String, Object> question = questionService.GetQuestion(id,false);
		if (question == null) {
			return Result.failure(ResultCode.QUESTION_NOT_EXIST);
		} else {
			if (questionService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取试题详情")
	@RequestMapping(value = "admin/question/{id}", method = RequestMethod.GET)
	public Result GetQuestionById(@PathVariable("id") Integer id) {
		Map<String, Object> question = questionService.GetQuestion(id,false);
		if (question == null) {
			return Result.failure(ResultCode.QUESTION_NOT_EXIST);
		} else {

			return Result.success(question);
		}
	}

	@ApiOperation("管理员修改试题状态")
	@RequestMapping(value = "admin/question/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateQuestionState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		Map<String, Object> data = questionService.GetQuestion(id,false);
		if (data == null) {
			return Result.failure(ResultCode.QUESTION_NOT_EXIST);
		} else {
			try {
				Question question = questionService.SetQuestionState(id, state);
				return Result.success(question);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索试题")
	@RequestMapping(value = "user/question/search", method = RequestMethod.GET)
	public Result UserSearchQuestion(@RequestParam("key") String key,@RequestParam(required = false,defaultValue = "-1")Integer id,@RequestParam(required = false,defaultValue = "-1")Integer type) {
		return Result.success(questionService.SearchQuestionWithKey(key, true,id,type));
	}

	@ApiOperation("管理员搜索试题")
	@RequestMapping(value = "admin/question/search", method = RequestMethod.GET)
	public Result AdminSearchQuestion(@RequestParam("key") String key,
			@RequestParam(required = false,defaultValue = "-1")Integer id,
			@RequestParam(required = false,defaultValue = "-1")Integer type) {
		return Result.success(questionService.SearchQuestionWithKey(key, false,id,type));
	}

	@ApiOperation("管理员获取试题列表")
	@RequestMapping(value = "admin/questions/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListQuestion(
			@PathVariable("page") Integer page,
			@PathVariable("count") Integer count,
			@RequestParam(value = "type",required = false,defaultValue = "0") Integer type,
			@RequestParam(value = "courseId",required = false,defaultValue = "0") Integer courseId) {
		if(type<1||type>4){
			type=null;
		}
	
		return Result.success(questionService.ListQuestionWithTagAndType(count, page, type, 0, false,courseId));
	}

	@ApiOperation("用户获取试题列表")
	@RequestMapping(value = "user/questions/{page}/{count}", method = RequestMethod.GET)
	public Result UserListQuestion(
			@PathVariable("page") Integer page,
			@PathVariable("count") Integer count,
			@RequestParam(value = "type",required = false,defaultValue = "0") Integer type,
			@RequestParam(value = "courseId",required = false,defaultValue = "0") Integer courseId) {
		return Result.success(questionService.ListQuestionWithTagAndType(count, page, type, 0, true,courseId));
	}
}
